package org.tigris.atlas.service.xml.schema;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.xml.stream.XMLStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tigris.atlas.service.xml.schema.longServiceResponse.LongListXMLDocument;

public class LongSchemaAssembler extends SchemaAssembler {

	private static Log LOG = LogFactory.getLog(LongSchemaAssembler.class);
	
	public Collection createLongList(LongListXMLDocument doc) {
		Collection out = new ArrayList();
		if (doc == null) {
			return out;
		}
		long [] in = doc.getLongListXML().getLongList().getLongArray();
		out.addAll(org.tigris.atlas.xml.schema.SchemaAssembler.transformInboundData(in));
		return out; 
	}	
	
	public XMLStreamReader createSchema(Collection in) { 
		long startTime = 0;
		if (LOG.isDebugEnabled()) {
			startTime = System.currentTimeMillis();
		}
		
		LongListXMLDocument xmlDocument = LongListXMLDocument.Factory.newInstance();
		xmlDocument.addNewLongListXML();
		xmlDocument.getLongListXML().addNewLongList();
		for (Iterator i = in.iterator(); i.hasNext();) {
			Long value = (Long) i.next();
			xmlDocument.getLongListXML().getLongList().addLong(org.tigris.atlas.xml.schema.SchemaAssembler.transformOutboundData(value));
		}

		XMLStreamReader reader = xmlDocument.getLongListXML().newXMLStreamReader();
		
		if (LOG.isDebugEnabled()) {
			long stopTime = System.currentTimeMillis();
			LOG.debug("Java to Schema transformation for Long list took: " + (stopTime - startTime) + "ms");			
		}		
				
		return reader;
	}
	
	protected Log getLog() {
		return LOG;
	}

}
